<?php
/**
 * Minhlog 后台删除功能控制器
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Controller
 * @package    Backend
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: DelController.php 11 2010-01-21 13:17:49Z Minh.Shu $
 *
 */

class Backend_DelController extends Minh_Controller_Action
{
    protected $_articleModel;
    protected $_categoryModel;
    protected $_tagModel;
    protected $_commentModel;
    protected $_pageModel;
    protected $_linkModel;
    protected $_userModel;

    public function init()
    {
        if (!isset($_SESSION['Minhlog']['storage']->user_id)) {
            $this->_redirect('/backend/user/login/');
        }

        if(null === $this->_articleModel){
            if(class_exists('Domain_Model_Article')){
                $this->_articleModel = new Domain_Model_Article();
            } else {
                throw new Exception('文章模型类不存在!');
            }
        }

        if(null === $this->_categoryModel){
            if(class_exists('Domain_Model_Category')){
                $this->_categoryModel = new Domain_Model_Category();
            } else {
                throw new Exception('文章分类模型类不存在!');
            }
        }

        if(null === $this->_tagModel){
            if(class_exists('Domain_Model_Tag')){
                $this->_tagModel = new Domain_Model_Tag();
            } else {
                throw new Exception('标签模型类不存在!');
            }
        }

        if(null === $this->_commentModel){
            if(class_exists('Domain_Model_Comment')){
                $this->_commentModel = new Domain_Model_Comment();
            } else {
                throw new Exception('文章评论模型类不存在!');
            }
        }

        if(null === $this->_pageModel){
            if(class_exists('Domain_Model_Page')){
                $this->_pageModel = new Domain_Model_Page();
            } else {
                throw new Exception('页面模型类不存在!');
            }
        }

        if(null === $this->_linkModel){
            if(class_exists('Domain_Model_Link')){
                $this->_linkModel = new Domain_Model_Link();
            } else {
                throw new Exception('友情链接模型类不存在!');
            }
        }

        if(null === $this->_userModel){
            if(class_exists('Domain_Model_User')){
                $this->_userModel = new Domain_Model_User();
            } else {
                throw new Exception('用户模型类不存在!');
            }
        }

        parent::init();
    }

    public function articleAction()
    {
        $filter = new Zend_Filter_Int();
        $articleId = $filter->filter($this->_getParam('id'));

        if($articleId > 0) $articleDel = $this->_articleModel->delArticle($articleId);

        $this->_redirect('/backend/list/article/');
    }

    public function categoryAction()
    {
        $filter = new Zend_Filter_Int();
        $categoryId = $filter->filter($this->_getParam('id'));

        if($categoryId > 0) $categoryDel = $this->_categoryModel->delCategory($categoryId);

        $this->_redirect('/backend/list/category/');
    }

    public function tagAction()
    {
        $filter = new Zend_Filter_Int();
        $tagId = $filter->filter($this->_getParam('id'));

        if($tagId > 0) $tagDel = $this->_tagModel->deltag($tagId);

        $this->_redirect('/backend/list/tag/');
    }

    public function linkAction()
    {
        $filter = new Zend_Filter_Int();
        $linkId = $filter->filter($this->_getParam('id'));

        if($linkId > 0) $linkDel = $this->_linkModel->delLink($linkId);

        $this->_redirect('/backend/list/link/');
    }

    public function linktypeAction()
    {
        $filter = new Zend_Filter_Int();
        $linkTypeId = $filter->filter($this->_getParam('id'));

        if($linkTypeId > 0) $linkTypeDel = $this->_linkModel->delLinkType($linkTypeId);

        $this->_redirect('/backend/list/linktype/');
    }

    public function commentAction()
    {
        $filter = new Zend_Filter_Int();
        $commentId = $filter->filter($this->_getParam('id'));

        if($commentId > 0) $commentDel = $this->_commentModel->delComment($commentId);

        $this->_redirect('/backend/list/comment/');
    }

    public function pageAction()
    {
        $filter = new Zend_Filter_Int();
        $pageId = $filter->filter($this->_getParam('id',0));

        if($pageId > 0 ) $pageDel = $this->_pageModel->delPage($pageId);

        $this->_redirect('/backend/list/page/');
    }
    
    public function cacheAction()
    {
        $this->cache->clean(Zend_Cache::CLEANING_MODE_ALL);
        $this->_redirect('/backend/index/index/');
    }
}